package com.atguigu.juc_day01.ticket_demo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Ticket{
    private int ticket = 60;
    Lock lock = new ReentrantLock(true);
    public void sale(){
        lock.lock ();
        if (ticket>0) System.out.println (Thread.currentThread ().getName ()+"购买了1张票，剩余"+--ticket+"张票了！");
        lock.unlock ();
    }
}
public class TrainTicketDemo2 {
    public static void main (String[] args) {
        Ticket ticket = new Ticket ();
        new Thread (()->{
            for (int i = 0; i < 51; i++) {
                ticket.sale ();
            }
        },"A").start ();
        new Thread(()->{
             for (int i = 0; i < 51; i++) {
               ticket.sale ();
            }
        },"B").start ();
        new Thread (()->{
            for (int i = 0; i < 51; i++) {
                ticket.sale ();
            }
        },"C").start ();
    }
}